#!/bin/bash
# Hopsan TLM start-up script
# Start with 6 arguments:
# 1 XModelName (XModel directory) 
# 2 start-time 
# 3 end-time 
# 4 max-time-step 
# 5 server-name:port 
# 6 model-file 

# *********************SETUP SIMULATION*********************************************
HopsanPath=$HOPSANHOME
Hopsan_exe="${HopsanPath}/bin/HopsanCLI"
Hopsan_args="--resultsFullCSV hopsanlogdata.csv"
Hopsan_cmd="${Hopsan_exe} ${Hopsan_args} -m ${PWD}/$1/$6 -e ${OMTLMSimulatorPath}/bin/Hopsan/TLMPluginLib/libTLMPluginLib.so -s $2,$4,$3 -l 1000"
  
TLMCONFIGFILE="tlm.config"

if [ ! -f "$1/$6" ] ; then 
    if [ ! -f "$1/$6.mo" ] ; then 
	echo Usage: 
	echo $0 XModelName start-time end-time max-time-step server-name:port model-file 
	echo $6
	pwd
	exit 1
    fi
fi

echo execution directory is $1
cd $1

echo Starting a Hopsan simulation with input file: $6
echo Make sure that: 
echo time = $2
echo timeEnd = $3
echo MaxTimeStep "<"= $4
echo 

echo Writing caseID $1 and server name $5 to file $TLMCONFIGFILE
echo $1 > $TLMCONFIGFILE 
echo $5 >> $TLMCONFIGFILE
echo $2 >> $TLMCONFIGFILE
echo $3 >> $TLMCONFIGFILE
echo $4 >> $TLMCONFIGFILE

echo Starting Hopsan
echo $Hopsan_cmd
echo DOING THIS: $Hopsan_Cmd > %1.simlog
$Hopsan_cmd >> $1.simlog
echo DONE  THIS: $Hopsan_Cmd >> %1.simlog


